package cn.zhaoguang.usedcar.main.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.zhaoguang.usedcar.common.page.PageRequest;
import cn.zhaoguang.usedcar.common.page.PageResult;
import cn.zhaoguang.usedcar.common.util.PageHelperUtil;
import cn.zhaoguang.usedcar.main.entity.Usedcar;
import cn.zhaoguang.usedcar.main.entity.dto.SellerInfoAndUsedcarDTO;
import cn.zhaoguang.usedcar.main.entity.dto.UsedcarDetailDTO;
import cn.zhaoguang.usedcar.main.entity.dto.UsedcarForManagerDTO;
import cn.zhaoguang.usedcar.main.entity.vo.UsedcarForManagerVO;
import cn.zhaoguang.usedcar.main.mapper.UsedcarMapper;
import cn.zhaoguang.usedcar.main.service.UsedcarService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description
 * @Author zhaoguang
 * @Date 2021/7/3 18:42
 */
@Service
public class UsedcarServiceImpl implements UsedcarService {

    @Autowired
    private UsedcarMapper usedcarMapper;

    @Override
    public int add(Usedcar usedcar) {
        usedcar.setCarSeries(usedcar.getCarSeries().toUpperCase());
        usedcar.setSearchName(usedcar.getCarBrand() + usedcar.getCarSeries());
        usedcar.setCreateTime(DateUtil.date());
        return usedcarMapper.insertSelective(usedcar);
    }

    @Override
    public int delete(Integer id) {
        return usedcarMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int update(Usedcar usedcar) {
        usedcar.setCarSeries(usedcar.getCarSeries().toUpperCase());
        usedcar.setSearchName(usedcar.getCarBrand() + usedcar.getCarSeries());
        return usedcarMapper.updateByPrimaryKeySelective(usedcar);
    }

    @Override
    public PageResult findAllBySellerId(PageRequest pageRequest) {
        //设置分页
        PageHelper.startPage(pageRequest.getCurrentPage(), pageRequest.getPageSize());
        //获取商家id和其它查询参数
        Integer sellerId = Convert.toInt(pageRequest.getParamByKey("sellerId"));
        String searchName = Convert.toStr(pageRequest.getParamByKey("searchName"));

        List<Usedcar> usedcarList = usedcarMapper.findAllBySellerId(sellerId, searchName);

        //分页结果
        PageResult result = PageHelperUtil.pageResult(new PageInfo<>(usedcarList));

        return result;
    }

    @Override
    public UsedcarDetailDTO findDetailById(Integer id) {
        return usedcarMapper.findDetailById(id);
    }

    @Override
    public Usedcar findUsedcarById(Integer id) {
        return usedcarMapper.selectByPrimaryKey(id);
    }

    @Override
    public PageResult findAll(PageRequest pageRequest) {
        //设置分页
        PageHelper.startPage(pageRequest.getCurrentPage(), pageRequest.getPageSize());
        //获取查询参数
        String searchName = Convert.toStr(pageRequest.getParamByKey("searchName"));

        List<Usedcar> usedcarList = usedcarMapper.findAll(searchName);

        //分页结果
        PageResult result = PageHelperUtil.pageResult(new PageInfo<>(usedcarList));
        return result;
    }

    @Override
    public PageResult choiceCar(PageRequest pageRequest) {
        //设置分页
        PageHelper.startPage(pageRequest.getCurrentPage(), pageRequest.getPageSize());
        //搜索、筛选
        String select = Convert.toStr(pageRequest.getParamByKey("select"));
        String value = Convert.toStr(pageRequest.getParamByKey("value"));

        List<Usedcar> usedcarList = null;

        switch (select) {
            case "searchName":
                usedcarList = usedcarMapper.findAll(value);
                break;
            case "def":
                if (value.equals("newest")) {
                    usedcarList = usedcarMapper.choiceCar("create_time", "DESC");
                }
                if (value.equals("priceLowest")) {
                    usedcarList = usedcarMapper.choiceCar("price", "ASC");
                }
                if (value.equals("priceHighest")) {
                    usedcarList = usedcarMapper.choiceCar("price", "DESC");
                }
                if (value.equals("mileageLowest")) {
                    usedcarList = usedcarMapper.choiceCar("drive_mileage", "ASC");
                }
                break;
            case "brand":
                System.out.println("品牌" + value);
                break;
            case "price":
                if (value.equals("unlimited")) {
                    usedcarList = usedcarMapper.findAll(null);
                }
                if (value.equals("below3")) {
                    usedcarList = usedcarMapper.choiceCarByPrice(0, 3);
                }
                if (value.equals("3to5")) {
                    usedcarList = usedcarMapper.choiceCarByPrice(3, 5);
                }
                if (value.equals("5to10")) {
                    usedcarList = usedcarMapper.choiceCarByPrice(5, 10);
                }
                if (value.equals("10to15")) {
                    usedcarList = usedcarMapper.choiceCarByPrice(10, 15);
                }
                if (value.equals("15to20")) {
                    usedcarList = usedcarMapper.choiceCarByPrice(15, 20);
                }
                if (value.equals("over20")) {
                    usedcarList = usedcarMapper.choiceCarByPrice(20, 1000);
                }
                break;
            case "carType":
                if (value.equals("unlimited")) {
                    usedcarList = usedcarMapper.findAll(null);
                }
                if (value.equals("mini")) {
                    usedcarList = usedcarMapper.choiceCarByType("微型车");
                }
                if (value.equals("small")) {
                    usedcarList = usedcarMapper.choiceCarByType("小型轿车");
                }
                if (value.equals("middleAndLarge")) {
                    usedcarList = usedcarMapper.choiceCarByType("中大型轿车");
                }
                if (value.equals("SUV")) {
                    usedcarList = usedcarMapper.choiceCarByType("SUV");
                }
                if (value.equals("MPV")) {
                    usedcarList = usedcarMapper.choiceCarByType("MPV");
                }
                if (value.equals("Van")) {
                    usedcarList = usedcarMapper.choiceCarByType("面包车");
                }
                if (value.equals("Pickup")) {
                    usedcarList = usedcarMapper.choiceCarByType("皮卡");
                }
                break;
        }
        //分页结果
        PageResult result = PageHelperUtil.pageResult(new PageInfo<>(usedcarList));
        return result;
    }

    @Override
    public SellerInfoAndUsedcarDTO findSellerInfoAndUsedcarByIdForUser(Integer id) {
        return usedcarMapper.findSellerInfoAndUsedcarByIdForUser(id);
    }

    @Override
    public List<Usedcar> findTodayUsedcar() {
        Date date = DateUtil.date();
        Date beginOfDay = DateUtil.beginOfDay(date);
        Date endOfDay = DateUtil.endOfDay(date);
        return usedcarMapper.findTodayUsedcar(beginOfDay, endOfDay);
    }


    @Override
    public PageResult findAllForManager(PageRequest pageRequest) {
        PageHelper.startPage(pageRequest.getCurrentPage(), pageRequest.getPageSize());
        //获取查询参数
        String searchName = Convert.toStr(pageRequest.getParamByKey("searchName"));

        List<UsedcarForManagerDTO> usedcarListDTO = usedcarMapper.findAllForManager(searchName);

        List<UsedcarForManagerVO> usedcarListVO = usedcarListDTO.stream().map(u -> {
            UsedcarForManagerVO usedcar = new UsedcarForManagerVO();
            //拷贝对象
            BeanUtil.copyProperties(u, usedcar);

            //处理标题图片和其他图片，并生成数组形式
            if (StrUtil.isNotBlank(u.getImgs())) {
                //标题图片和其他图片都有时
                String str = u.getImg() + "," + u.getImgs();
                String[] arr = str.split(",");
                usedcar.setImgs(Arrays.asList(arr));
            } else {
                //只有标题图片时
                List<String> strList = new ArrayList<>();
                strList.add(u.getImg());
                usedcar.setImgs(strList);
            }
            //返回数据
            return usedcar;
        }).collect(Collectors.toList());

        //分页结果
        PageResult result1 = PageHelperUtil.pageResult(new PageInfo<>(usedcarListDTO));
        PageResult result2 = PageHelperUtil.pageResult(new PageInfo<>(usedcarListVO));
        result2.setCurrentPage(result1.getCurrentPage());
        result2.setPageSize(result1.getPageSize());
        result2.setTotalSize(result1.getTotalSize());
        result2.setTotalPages(result1.getTotalPages());

        //返回结果给前端
        return result2;
    }
}
